package com.bJian.bJIanOJ.judge.codeSandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.bJian.bJIanOJ.judge.codeSandbox.CodeSandbox;
import com.bJian.bJIanOJ.judge.codeSandbox.model.ExecuteCodeRequest;
import com.bJian.bJIanOJ.judge.codeSandbox.model.ExecuteCodeResponse;
import com.bJian.bJIanOJ.model.dto.question_submit.JudgeInfo;

/**
 * 远程代码沙箱实现
 */
public class RemoteCodeSandboxImpl implements CodeSandbox {

    //定义鉴权请求头和密匙
    private static final String AUTH_REQUEST_HEADER = "auth";

    private static final String AUTH_REQUEST_SECRET = "BJOJAPI_0101SfAc";

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        //http://8.138.136.35/
        String url = "http://localhost:8190/executeCode";
        String jsonBody = JSONUtil.toJsonStr(executeCodeRequest);

        String result = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET)
                .body(jsonBody)
                .execute()
                .body();

        System.out.println(result);

        ExecuteCodeResponse executeCodeResponse = JSONUtil.toBean(result, ExecuteCodeResponse.class);
        return executeCodeResponse;

    }
}
